package com.dbh.company.buildbook;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

/**
 * @author dongbanghui
 * @create 2023/10/17
 */
public class SunlightAnalysis {

    private static final String  cookie = "crosSdkDT2019DeviceId=-uudqok-p0z6tw-u5tozzmsej9s39l-t6zeqcw9i; _ga=GA1.2.2121015128.1686129466; wx_device_id=bUTx-Zs85VX-EJ38i6V8ZKBFvZtdlNd_7n7X1cq3OrE=; ke_uuid=f975736ecd03071f59f4d5c22f49dd01; __xsptplus788=788.3.1702262037.1702262277.5%234%7C%7C%7C%7C%7C%23%23%23; _ga_F0DQ6TTSVW=GS1.2.1706079043.106.1.1706079084.19.0.0; lianjia_b_token=2.001027608585b684a8018ede37533f7467; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%22ce301845-8fe2-4a00-96fb-8f5ac09fbf35%22%2C%22first_id%22%3A%2218976818cbd94a-066522c3f35ba78-7e565470-2073600-18976818cbe2427%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E8%87%AA%E7%84%B6%E6%90%9C%E7%B4%A2%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC%22%2C%22%24latest_referrer%22%3A%22https%3A%2F%2Fcn.bing.com%2F%22%2C%22%24latest_referrer_host%22%3A%22cn.bing.com%22%7D%2C%22%24device_id%22%3A%2218976818cbd94a-066522c3f35ba78-7e565470-2073600-18976818cbe2427%22%7D; login_ucid=1000000029138498; _account_=dongbanghui001; _userFullEmail_=dongbanghui001%40ke.com; security_ticket=BiiaWCizdSmGaY89837Kn2znfH3Q/nCfoKLhAYKDWulT+mpyxW50IiwDbwh6qj3dbbQ+LqGfHmhVTBbN1AbO8DP7vki+Fe5Mtd/NSfiQFJqK9SAGB2+F/SsmWVIDCUepLai85/NDF0iurIdg2OyjLSh6Z0R88+guwBC79j/tcGQ=; ztt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJhX2NsaWVudCIsImV4cCI6MTcwODY1NjEwOCwianRpIjoiT2l2L0VaUk9FZzAwcVlsZFA0ZlhkZz09IiwiaWF0IjoxNzA4NTY5NzA4LCJpc3MiOiJlMWE3YWMxM2RmOWI0MmQzYmM5NmRjNzJjYjAxMzVmZCIsInIiOiJ7XCJpXCI6XCIxMC40LjYwLjI4XCIsXCJ1XCI6XCIxMDAwMDAwMDI5MTM4NDk4XCIsXCJzXCI6XCJTVC0xMjI4MDU0OS1sOFQ3blpNd3dqVHhDcVRNUjJJTVA2MU4tWlEta2UuY29tXCIsXCJhXCI6MSxcImVcIjoxNzA4NjU2MTA4LFwibFwiOi0xLFwiZFwiOjAsXCJlaVwiOlwiXCIsXCJpaVwiOlwiMTAuNC42MC4yOFwifSJ9.vEN-6hSahDwEma-1U-Qgidw7bbU0bZ7acQKbSDS_WTQ; lianjia_token=2.0111a9beb284385a9f0001cb5454bc2a22972044b6; casgwusercred=JeB0aqScXfnBqHHKMTc-uGpJVi6ZZ1nUVn2pHmKBo7rUpT7l-rLFQd-XEzym67bxy0pXYvm_d2kFU4CB9RsMi1QiKnJ2qMQ7zKW2RI6BmRdfHuRB9qiSSICSgIYtUtF7iqMtcH7q0zCi_15TI0C4bT3XOYiuW6vmYO0R27jmpIg; crosgwusercred=P9_5unP3MN3GId8ENxx3RY9uU-nj4kU9Fn2168bOqP8sxHwPpu4GZfyNevKWphxJKJlfp7xdmGd4r-p0rYOLVw359aa90504e0bb2521a0cb2e946d860e; pilot_ke_token=2.0111a9beb284385a9f0001cb5454bc2a22972044b6; mr_backend_login_user=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzZXNzaW9uSWQiOiIyLjAwMTQ3OWEyNGM4MWU4NDY2MTA1ZDAxY2ZlZWRlNjg4ZjgifQ.UflIa0U47XgdrUJmLpbJYQNOVWUmdRYSwsOwS57IQPU; mr_backend_service_uid=1000000029138498; lianjia_uuid=6f614d67-edf5-48ce-9004-3de687191c69; lianjia_ssid=8d5a4238-6cbe-4664-8654-16034cb16719";
    public static void main(String[] args) {
        houdiniFinish("665287");

    }

    public static String projects() {
        Integer pageNo = 1;
        String url = "https://backend-mr.ke.com/v1/projects?type=SUNLIGHT%2CSUNLIGHT_ESF&status=5&pageNo="+pageNo+"&pageSize=20";
        String json = HttpRequest.get(url)
                .header("Cookie", cookie)
                .execute()
                .body();
        JSONObject jsonObject = JSONUtil.parseObj(json);
        Object dataObject = jsonObject.get("data");
        Object listObject = JSONUtil.parseObj(JSONUtil.toJsonStr(dataObject)).get("list");
        Object totalObject = JSONUtil.parseObj(JSONUtil.toJsonStr(dataObject)).get("total");
        Integer total = Convert.toInt(totalObject);
        JSONArray listArray = JSONUtil.parseArray(listObject);
        Object[] array = listArray.toArray();
        String projectNumStr = "";
        for (int i = 0; i < array.length; i++) {
            Object o = array[i];
            JSONObject jsonObject1 = JSONUtil.parseObj(o);
            String updateTime = jsonObject1.get("updateTime").toString();
            projectNumStr += jsonObject1.get("projectNum").toString() + ",";
        }

        int loop = Convert.toInt(Math.ceil(total / 15));
        for (int i = 0; i < loop; i++) {
            pageNo = i + 2;
            url = "https://backend-mr.ke.com/v1/projects?type=SUNLIGHT%2CSUNLIGHT_ESF&status=5&pageNo="+
                    pageNo + "&pageSize=20";
            json = HttpRequest.get(url)
                    .header("Cookie", cookie)
                    .execute()
                    .body();
            jsonObject = JSONUtil.parseObj(json);
            dataObject = jsonObject.get("data");
            listObject = JSONUtil.parseObj(JSONUtil.toJsonStr(dataObject)).get("list");
            totalObject = JSONUtil.parseObj(JSONUtil.toJsonStr(dataObject)).get("total");
            listArray = JSONUtil.parseArray(listObject);
            array = listArray.toArray();
            for (int j = 0; j < array.length; j++) {
                Object o = array[j];
                JSONObject jsonObject1 = JSONUtil.parseObj(o);
                String updateTime = jsonObject1.get("updateTime").toString();
                projectNumStr += jsonObject1.get("projectNum").toString() + ",";
            }
        }
        projectNumStr = projectNumStr.substring(0, projectNumStr.length()-1);
        Console.log(projectNumStr);
        return projectNumStr;
    }


    public static void setBaseImg(String projectNumStr) {
        String[] projectNumArray = projectNumStr.split(",");
        for (String projectNum : projectNumArray) {
            String url = "https://backend-mr.ke.com//v1/projects/sunlight/set-img";
            String body = HttpRequest.post(url)
                    .header("Cookie", cookie)
                    .form("projectNum", projectNum)
                    .execute().body();
            Console.log(body);
        }
    }

    /**
     * 日照分析重试
     * @param projectNum
     */
    public static void retry(String projectNum){
        String url = "https://backend-mr.ke.com/v1/projects/sunlight/retry";
        String body = HttpRequest.post(url)
                .header("Cookie", cookie)
                .form("projectNum", projectNum)
                .execute().body();
        Console.log(body);
    }

    /**
     * 处理模型生成中的项目
     * t_entity_project的status为5 和t_sunlight_analysis表中status为MODEL_CONSTRUCT
     * @param projectNum
     */
    public static String houdiniFinish(String projectNum) {
        String url = "https://backend-mr.ke.com/inner/project/sunlight/houdini-finish";
        String body = HttpRequest.post(url)
                .header("Cookie", cookie)
                .form("projectNum", projectNum, "status", "MODEL_CONSTRUCT_FAIL", "retry", "true")
                .execute().body();
        Console.log(body);
        return body;
    }

    /**
     * 处理分析已完成的项目
     * x
     * @param projectNum
     */
    public static void houdini(String projectNum) {
        String url = "https://backend-mr.ke.com//v1/projects/sunlight/houdini";
        String body = HttpRequest.post(url)
                .header("Cookie", cookie)
                .form("projectNum", projectNum)
                .execute().body();
        Console.log(body);
    }


}


